package com.yicong.ants.utils;

import com.cchao.simplelib.core.Logs;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * @author cchao
 * @version 2019-10-16.
 */
public class TimeHelper {
    /**
     * 将时间转换为时间戳
     */
    public static long formatToStamp(String formatTime) throws Exception {
        return formatToStamp(formatTime, "yyyy-MM-dd HH:mm:ss");
    }

    /**
     * 将时间转换为时间戳
     */
    public static long formatToStamp(String s, String format) throws Exception {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        Date date = simpleDateFormat.parse(s);
        return date.getTime();
    }

    /**
     * 将时间戳转换为时间
     */
    public static String stampToFormat(String timeStamp, String format) throws Exception {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        Date date = new Date(Long.valueOf(timeStamp));
        res = simpleDateFormat.format(date);
        return res;
    }

    /**
     * 将时间戳转换为时间
     */
    public static String stampToFormat(String timeStamp) throws Exception {
        return stampToFormat(timeStamp, "yyyy-MM-dd HH:mm:ss");
    }

    /**
     * 格式化当前时间
     */
    public static String formatCurTime(String format) {
        try {
            return stampToFormat(String.valueOf(System.currentTimeMillis()), format);
        } catch (Exception ex) {
            Logs.logException(ex);
        }
        return "2022-02-02";
    }

    /**
     * 格式化明天时间
     */
    public static String formatNextTime(String format) {
        try {
            SimpleDateFormat formatter = new SimpleDateFormat(format, Locale.CHINA);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(new Date());
            calendar.add(Calendar.DATE, 1);
            return formatter.format(calendar.getTime());
        } catch (Exception ex) {
            Logs.logException(ex);
        }
        return "2022-02-02";
    }

    /**
     * 两个日期相隔天数
     *
     * @param startDate
     * @param endDate
     * @return
     */
    public static String differentDays(String startDate, String endDate) {
        try {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            Date date1 = formatter.parse(startDate); // 将得到的日期字符串转换为日期类型
            Date date2 = formatter.parse(endDate);
            long day = Math.abs(date2.getTime() - date1.getTime());//得到的两个日期的毫秒数大小
            day /= (1000 * 60 * 60 * 24);
            return String.valueOf(day);
        } catch (Exception e) {
            e.printStackTrace();
            return "0";
        }
    }

    /**
     * 两个日期相隔天数
     *
     * @param startDate
     * @param endDate
     * @return
     */
    public static boolean isSameDay(long leftDate, long rightDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
        return formatter.format(new Date(leftDate)).equals(formatter.format(new Date(rightDate)));
    }

    /**
     * 日期格式转换
     *
     * @param time
     * @param oldFormat
     * @param newFormat
     * @return
     */
    public static String getFormattedTime(String time, String oldFormat, String newFormat) {
        try {
            return (new SimpleDateFormat(newFormat)).format((new SimpleDateFormat(oldFormat)).parse(time));
        } catch (ParseException e) {
            e.printStackTrace();
            return "";
        }
    }
}
